package com.example.qwen02;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class BackGroundPost {
    public static void main(String[] args) throws Exception {
        // 创建HttpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建HttpPost对象，指定URL
        String url = "https://dashscope.aliyuncs.com/api/v1/services/aigc/background-generation/generation/";
        HttpPost httpPost = new HttpPost(url);

        // 设置请求头，包括Content-Type为application/json
        httpPost.setHeader("X-DashScope-Async", "enable");
        httpPost.setHeader("Authorization", "Bearer"+System.getenv("DASHSCOPE_API_KEY"));
        httpPost.setHeader("Content-type", "application/json");



        // JSON参数
        String jsonParam = "{\n" +
                "    \"model\": \"wanx-background-generation-v2\",\n" +
                "    \"input\": {\n" +
                "        \"base_image_url\": \"https://vision-poster.oss-cn-shanghai.aliyuncs.com/lllcho.lc/data/test_data/images/main_images/new_main_img/a.png\",\n" +
                "        \"ref_image_url\": \"https://vision-poster.oss-cn-shanghai.aliyuncs.com/lllcho.lc/data/test_data/images/ref_images/d1faf4f26c8c4ea798d043a8bf3784bb_2.png\",\n" +
                "        \"ref_prompt\": \"放在布满苔藓的土地上，被蕨类植物叶片包围，背景是茂盛的植被，丰富的光影细节\",\n" +
                "        \"reference_edge\": {\n" +
                "            \"foreground_edge\": [\n" +
                "                \"https://vision-poster.oss-cn-shanghai.aliyuncs.com/lllcho.lc/data/test_data/images/huaban_soft_edge/6cdd13941cef1b11d885aea1717b983ae566b8efc9094-vcsvxa_fw658webp.png\"\n" +
                "            ]\n" +
                "        }\n" +
                "    },\n" +
                "    \"parameters\": {\n" +
                "        \"n\": 4,\n" +
                "        \"noise_level\": 300,\n" +
                "        \"ref_prompt_weight\": 0.5,\n" +
                "        \"scene_type\": \"GENERAL\"\n" +
                "    }\n" +
                "}";

        // 将JSON字符串转换为HttpEntity对象并设置到HttpPost中
        httpPost.setEntity(new StringEntity(jsonParam));

        try {
            // 执行HTTP POST请求
            CloseableHttpResponse response = httpClient.execute(httpPost);

            try {
                // 获取响应状态码
                int statusCode = response.getStatusLine().getStatusCode();
                System.out.println("Status Code: " + statusCode);

                // 如果请求成功，读取响应实体
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        String result = EntityUtils.toString(entity);
                        System.out.println("Response: " + result);
                    }
                } else {
                    System.out.println("Request failed with status code: " + statusCode);
                }
            } finally {
                response.close();
            }
        } finally {
            httpClient.close();
        }
    }
}
